package com.inspinia.config.common.config;

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ResourceBundle;

/**
 * elk配置
 * 优先使用配置中心的配置，如果没有配置文件使用本地配置
 * @author Veblen
 */
@Slf4j
@Data
public class LogbackRabbitConfig {

    /**
     * kafka.properties的配置数据
     */
    public static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle("logback-rabbitmq");
    public static String getString(String key) {
        return RESOURCE_BUNDLE.getString(key);
    }

    public static String address = getString("rabbitmq.logback.addresses");
    public static String username = getString("rabbitmq.logback.username");
    public static String password = getString("rabbitmq.logback.password");
    public static String declareExchange = getString("rabbitmq.logback.declareExchange");
    public static String exchangeType = getString("rabbitmq.logback.exchangeType");
    public static String exchangeName = getString("rabbitmq.logback.exchangeName");
    public static String routingKeyPattern = getString("rabbitmq.logback.routingKeyPattern");
    public static String generateId = getString("rabbitmq.logback.generateId");
    public static String charset = getString("rabbitmq.logback.charset");
    public static String durable = getString("rabbitmq.logback.durable");
    public static String deliveryMode = getString("rabbitmq.logback.deliveryMode");
    public static String autoDelete = getString("rabbitmq.logback.autoDelete");


    /**
     *  监听配置
     * @param changeEvent
     */
    @ApolloConfigChangeListener("logback-rabbitmq")
    public void logbackRabbitChange(ConfigChangeEvent changeEvent){
        log.info("Changes for namespace {}", changeEvent.getNamespace());
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            log.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                    change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                    change.getChangeType());
            if ("rabbitmq.logback.addresses".equals(change.getPropertyName())) {
                address = change.getNewValue();
            }if ("rabbitmq.logback.username".equals(change.getPropertyName())) {
                username = change.getNewValue();
            }if ("rabbitmq.logback.password".equals(change.getPropertyName())) {
                password = change.getNewValue();
            }if ("rabbitmq.logback.declareExchange".equals(change.getPropertyName())) {
                declareExchange = change.getNewValue();
            }if ("rabbitmq.logback.exchangeType".equals(change.getPropertyName())) {
                exchangeType = change.getNewValue();
            }if ("rabbitmq.logback.exchangeName".equals(change.getPropertyName())) {
                exchangeName = change.getNewValue();
            }if ("rabbitmq.logback.routingKeyPattern".equals(change.getPropertyName())) {
                routingKeyPattern = change.getNewValue();
            }if ("rabbitmq.logback.generateId".equals(change.getPropertyName())) {
                generateId = change.getNewValue();
            }if ("rabbitmq.logback.charset".equals(change.getPropertyName())) {
                charset = change.getNewValue();
            }if ("rabbitmq.logback.durable".equals(change.getPropertyName())) {
                durable = change.getNewValue();
            }if ("rabbitmq.logback.deliveryMode".equals(change.getPropertyName())) {
                deliveryMode = change.getNewValue();
            }if ("rabbitmq.logback.autoDelete".equals(change.getPropertyName())) {
                autoDelete = change.getNewValue();
            }
        }
    }
}
